﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RTW.Servers;

namespace RTW.Web.Comm
{
    [Route("/[controller]/[Action]/{ID?}")]
    [ApiController]
    public class EditDataController:ControllerBase
    {
        public SysLogServer sysLogServer;
        public EditDataController() {
            sysLogServer = new SysLogServer();
        }
        [HttpPost]
        public async Task<int> ChangeState()
        {
            string TabName = !string.IsNullOrEmpty(Request.Form["TabName"].ToString())? Request.Form["TabName"].ToString() : "";//表名
            string ID = !string.IsNullOrEmpty(Request.Form["ID"].ToString()) ? Request.Form["ID"].ToString() : "";//编号值
            string ParName = !string.IsNullOrEmpty(Request.Form["ParName"].ToString()) ? Request.Form["ParName"].ToString() : "";//参数名
            string ParVal = !string.IsNullOrEmpty(Request.Form["ParVal"].ToString()) ? Request.Form["ParVal"].ToString() : "";//参数值
            ParVal = (ParVal == "0" ? "1" : "0");

            string sql = string.Format("update {0} set {1}={2} where ID='{3}';", TabName, ParName, ParVal, ID);
            
            return await sysLogServer.ExecSqlAsync(sql);
        }

        public async Task<int> DelData()
        {
            string TabName = !string.IsNullOrEmpty(Request.Form["TabName"].ToString()) ? Request.Form["TabName"].ToString() : "";//sql
            string ColID = !string.IsNullOrEmpty(Request.Form["ColID"].ToString()) ? Request.Form["ColID"].ToString() : "";//sql

            string SqlStr = string.Format("delete from {0} where ID='{1}';", TabName, ColID);
            return await sysLogServer.ExecSqlAsync(SqlStr);
        }

        public async Task<int> logicalDelData()
        {
            string TabName = !string.IsNullOrEmpty(Request.Form["TabName"].ToString()) ? Request.Form["TabName"].ToString() : "";//sql
            string ColID = !string.IsNullOrEmpty(Request.Form["ColID"].ToString()) ? Request.Form["ColID"].ToString() : "";//sql

            string SqlStr = string.Format("update {0} set IsDelete='1' where ID='{1}'", TabName, ColID);
            return await sysLogServer.ExecSqlAsync(SqlStr);
        }
    }
}
